package com.zxm.sunshineaiary.view.myedittext;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.RectF;
import android.util.AttributeSet;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;

import com.zxm.sunshineaiary.activity.R;

public class FingerPaint extends View {
	private String TAG = " FingerPaint";

	private static final float TOUCH_TOLERANCE = 4;
	private final int TEST_SIZE_AND_MARGINS = 13;
	private final int BITMAP_SIZE = 80;

	private Bitmap mBitmap = null;
	private Canvas mCanvas = null;
	private Path mPath = null;// 用于把Line画在Bitmap图板上
	private Path mImagePath = null;// 用于记录轨迹，方便剪切
	private Paint mBitmapPaint = null;
	private Paint mPaint = null;

	private int backgroundColor = 0;
	private int drawLineColor = 0;
	private int marginsX;
	private float mX, mY;

	public FingerPaint(Context context, AttributeSet attrs, int defStyle) {
		super(context, attrs, defStyle);
		init();
		setPropertyOnXml(context, attrs);
	}

	public FingerPaint(Context context, AttributeSet attrs) {
		super(context, attrs);
		init();
		setPropertyOnXml(context, attrs);
	}

	public FingerPaint(Context context) {
		super(context);
		init();
	}

	@Override
	protected void onSizeChanged(int w, int h, int oldw, int oldh) {

		marginsX = w - TEST_SIZE_AND_MARGINS;
		mBitmap = Bitmap.createBitmap(w, h, Bitmap.Config.ARGB_8888);
		mCanvas = new Canvas(mBitmap);
		super.onSizeChanged(w, h, oldw, oldh);
	}

	private void init() {
		mPath = new Path();
		mImagePath = new Path();
		mBitmapPaint = new Paint(Paint.DITHER_FLAG);
		mBitmapPaint.setAntiAlias(true);
		mBitmapPaint.setDither(true);
		setPaint();
	}

	private void setPropertyOnXml(Context context, AttributeSet attrs) {
		TypedArray ta = context.obtainStyledAttributes(attrs,
				R.styleable.FingerPaint);
		backgroundColor = ta.getColor(R.styleable.FingerPaint_background, 0);
	}

	private void setPaint() {
		mPaint = new Paint();
		mPaint.setAntiAlias(true);
		mPaint.setDither(true);
		mPaint.setColor(drawLineColor);
		mPaint.setStyle(Paint.Style.STROKE);
		mPaint.setStrokeJoin(Paint.Join.ROUND);
		mPaint.setStrokeCap(Paint.Cap.ROUND);
		mPaint.setStrokeWidth(TEST_SIZE_AND_MARGINS);
	}

	@Override
	protected void onDraw(Canvas canvas) {
		canvas.drawColor(backgroundColor);
		canvas.drawBitmap(mBitmap, 0, 0, mBitmapPaint);
		canvas.drawPath(mPath, mPaint);
		super.onDraw(canvas);
	}

	private void touch_start(float x, float y) {
		mPath.moveTo(x, y);
		mImagePath.moveTo(x, y);
		mX = x;
		mY = y;
	}

	private void touch_move(float x, float y) {

		float dx = Math.abs(x - mX);
		float dy = Math.abs(y - mY);
		if (dx >= TOUCH_TOLERANCE || dy >= TOUCH_TOLERANCE) {
			mPath.quadTo(mX, mY, (x + mX) / 2, (y + mY) / 2);
			mImagePath.quadTo(mX, mY, (x + mX) / 2, (y + mY) / 2);
			mX = x;
			mY = y;
		}
	}

	private void touch_up() {
		mPath.lineTo(mX, mY);
		mImagePath.lineTo(mX, mY);
		mCanvas.drawPath(mPath, mPaint);
		mPath.reset();
	}

	@Override
	public boolean onTouchEvent(MotionEvent event) {
		float x = event.getX();
		float y = event.getY();

		switch (event.getAction()) {
		case MotionEvent.ACTION_DOWN:
			if (x > TEST_SIZE_AND_MARGINS && x < marginsX) {
				touch_start(x, y);
				invalidate();
			}
			break;
		case MotionEvent.ACTION_MOVE:
			if (x > TEST_SIZE_AND_MARGINS && x < marginsX) {
				touch_move(x, y);
				invalidate();
			}
			break;
		case MotionEvent.ACTION_UP:
			touch_up();
			invalidate();
			break;
		}
		return true;
	}

	/**
	 * ��ջ���
	 */
	public void clearCanvas() {
		// mPath.reset();
		mImagePath.reset();
		mBitmap.eraseColor(Color.TRANSPARENT);
		invalidate();
	}

	public Bitmap getBitmap() {
		Log.v(TAG, "1");
		RectF rf = new RectF();
		if (mImagePath.isEmpty()) {
			return null;
		}
		mImagePath.computeBounds(rf, false);

		int x = (int) rf.left - TEST_SIZE_AND_MARGINS;
		int y = this.getScrollY();
		int width = (int) (rf.right + TEST_SIZE_AND_MARGINS) - x;
		int height = this.getHeight();

		Bitmap b = Bitmap.createBitmap(mBitmap, x, y, width, height);
		return lessenBitmap(b, BITMAP_SIZE);
	}

	/**
	 * lessen the bitmap
	 * 
	 * @param src
	 *            bitmap
	 * @param d
	 *            the dest bitmap width
	 * @param e
	 * @return new bitmap if successful ,oherwise null
	 */
	private Bitmap lessenBitmap(Bitmap src, int d) {
		if (src == null) {
			return null;
		}
		int w = src.getWidth();
		int h = src.getHeight();
		float scaleHeight = ((float) d) / h;
		Matrix m = new Matrix();
		m.postScale(scaleHeight, scaleHeight);
		Bitmap resizedBitmap = Bitmap.createBitmap(src, 0, 0, w, h, m, true);
		src.recycle();
		return resizedBitmap;
	}

	/**
	 * 设置画笔颜色
	 * 
	 * @param color
	 */
	public void setDrawLineColor(int color) {
		drawLineColor = color;
		mPaint.setColor(drawLineColor);
	}
}
